package com.gkzf.ai.module.crm.service.product.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkzf.ai.framework.common.pojo.PageResult;
import com.gkzf.ai.framework.common.util.object.BeanUtils;
import com.gkzf.ai.module.crm.controller.admin.product.vo.repayment.CrmProductRepaymentListReqVO;
import com.gkzf.ai.module.crm.controller.admin.product.vo.repayment.CrmProductRepaymentPageReqVO;
import com.gkzf.ai.module.crm.controller.admin.product.vo.repayment.CrmProductRepaymentSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.product.CrmProductRepaymentDO;
import com.gkzf.ai.module.crm.dal.mysql.product.CrmProductRepaymentMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.gkzf.ai.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.gkzf.ai.module.crm.enums.ErrorCodeConstants.PRODUCT_REPAYMENT_NOT_EXISTS;

/**
 * @author Huawei
 * @date 2025-03-31 15:09
 */
@Service
@Validated
public class CrmProductRepaymentServiceImpl implements CrmProductRepaymentService {

    @Resource
    private CrmProductRepaymentMapper crmProductRepaymentMapper;

    @Override
    public Long createProductRepayment(CrmProductRepaymentSaveReqVO createReqVO) {
        // 插入
        CrmProductRepaymentDO productRepayment = BeanUtils.toBean(createReqVO, CrmProductRepaymentDO.class);
        crmProductRepaymentMapper.insert(productRepayment);
        // 返回
        return productRepayment.getId();
    }

    @Override
    public void createBatchProductRepayment(List<CrmProductRepaymentSaveReqVO> createReqVOs) {
        // 2. 插入分类
        List<CrmProductRepaymentDO> categoryList = BeanUtils.toBean(createReqVOs, CrmProductRepaymentDO.class);
        crmProductRepaymentMapper.insertBatch(categoryList);
    }

    @Override
    public void updateProductRepayment(CrmProductRepaymentSaveReqVO updateReqVO) {
        // 校验存在
        validateProductRepaymentExists(updateReqVO.getId());
        // 更新
        CrmProductRepaymentDO updateObj = BeanUtils.toBean(updateReqVO, CrmProductRepaymentDO.class);
        crmProductRepaymentMapper.updateById(updateObj);
    }

    /**
     *  对比获取新增对象和删除对象
     * @param updateReqVO 更新信息
     * @param productId 产品id
     */
    @Override
    public void updateProductRepaymentByProductId(List<CrmProductRepaymentSaveReqVO> updateReqVO,Long productId) {
        LambdaQueryWrapper<CrmProductRepaymentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CrmProductRepaymentDO::getProductId,productId);
        List<CrmProductRepaymentDO> repaymentDOList = crmProductRepaymentMapper.selectList(queryWrapper);
        List<String> noVoList = updateReqVO.stream().map(CrmProductRepaymentSaveReqVO::getNo).toList();
        List<String> noDoList = repaymentDOList.stream().map(CrmProductRepaymentDO::getNo).toList();
        // 获取新增对象
        List<CrmProductRepaymentSaveReqVO> addRepayment = updateReqVO.stream().filter(l -> !noDoList.contains(l.getNo())).toList();
        List<CrmProductRepaymentDO> addRepaymentDo = addRepayment.stream().map(r -> {
            CrmProductRepaymentDO repaymentListReqDO = new CrmProductRepaymentDO();
            BeanUtils.copyProperties(r, repaymentListReqDO);
            repaymentListReqDO.setProductId(productId);
            return repaymentListReqDO;
        }).toList();
        // 获取删除对象
        List<CrmProductRepaymentDO> deleteRepayment = repaymentDOList.stream().filter(l -> !noVoList.contains(l.getNo())).toList();
        // 批量新增和删除
        crmProductRepaymentMapper.insertBatch(addRepaymentDo);
        crmProductRepaymentMapper.deleteByIds(deleteRepayment.stream().map(CrmProductRepaymentDO::getId).toList());
    }

    @Override
    public void deleteProductRepayment(Long id) {
        // 校验存在
        validateProductRepaymentExists(id);
        // 删除
        crmProductRepaymentMapper.deleteById(id);
    }

    private void validateProductRepaymentExists(Long id) {
        if (crmProductRepaymentMapper.selectById(id) == null) {
            throw exception(PRODUCT_REPAYMENT_NOT_EXISTS);
        }
    }

    @Override
    public CrmProductRepaymentDO getProductRepayment(Long id) {
        return crmProductRepaymentMapper.selectById(id);
    }

    @Override
    public List<CrmProductRepaymentDO> getProductRepaymentListAll() {
        return crmProductRepaymentMapper.selectList();
    }

    @Override
    public List<CrmProductRepaymentListReqVO> getProductRepaymentListVoAll(List<CrmProductRepaymentDO> repaymentDOList,Long productId) {
        // 获取对应数据
        List<CrmProductRepaymentDO> repaymentVoList = repaymentDOList.stream().filter(p -> p.getProductId().equals(productId)).toList();
        // 还款对象 转换 DO -> VO
        return repaymentVoList.stream().map(r -> {
            CrmProductRepaymentListReqVO repaymentListReqVO = new CrmProductRepaymentListReqVO();
            BeanUtils.copyProperties(r, repaymentListReqVO);
            return repaymentListReqVO;
        }).toList();
    }

    @Override
    public List<CrmProductRepaymentListReqVO> getProductRepaymentByProductId(Long productId) {
        LambdaQueryWrapper<CrmProductRepaymentDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CrmProductRepaymentDO::getProductId, productId);
        List<CrmProductRepaymentDO> repaymentDOList = crmProductRepaymentMapper.selectList(lambdaQueryWrapper);
        return getProductRepaymentListVoAll(repaymentDOList, productId);
    }

    @Override
    public PageResult<CrmProductRepaymentDO> getProductRepaymentPage(CrmProductRepaymentPageReqVO pageReqVO) {
        return crmProductRepaymentMapper.selectPage(pageReqVO);
    }

}

